package com.rabbit.orderservice.config;


import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class OrderConfig {

    // 创建队列和交换机
    @Bean("orderQueue")
    public Queue orderQueue() {
        return QueueBuilder.durable("order_queue").build();
    }

    @Bean("orderExchange")
    public DirectExchange orderExchange() {
        return ExchangeBuilder.directExchange("order-exchange").durable(true).build();
    }

    @Bean("orderBinding")
    public Binding orderBinding(@Qualifier("orderQueue") Queue queue,@Qualifier("orderExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).withQueueName();
    }

    @Bean
    public Jackson2JsonMessageConverter Jackson2JsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory,Jackson2JsonMessageConverter jsonMessageConverter) {
        RabbitTemplate rabbitTemplate=new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(jsonMessageConverter);
        return rabbitTemplate;
    }

}
